#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

#include "../Log/ZLOG/src/zlog.h"

#define __MODULE__ "HOST_DEMO"
static int SIGNAL_COUGHT;


void signal_handler (int signal)
{
	//printf ("Signal %d caught, exiting\n",signal);

	SIGNAL_COUGHT = 1;
	exit (0);
}

void * signal_set (int signo, void (*func)(int))
{
	int ret;
  	struct sigaction sig;
  	struct sigaction osig;

  	sig.sa_handler = func;
  	sigemptyset (&sig.sa_mask);
  	sig.sa_flags = 0;
#ifdef SA_RESTART
  	sig.sa_flags |= SA_RESTART;
#endif /* SA_RESTART */

  	ret = sigaction (signo, &sig, &osig);

  	if (ret < 0) 
    		return (SIG_ERR);
  	else
    		return (osig.sa_handler);
}
void
signal_init ()
{
 	//signal_set (SIGINT, sigint);
  	//signal_set (SIGTERM, sigint);
  
  	//SIGPIPE signal be ignored
 	//write on a pipe with no one to read it 
  	signal_set (SIGPIPE, SIG_IGN);
}

/***********************************************************
*
*
*
************************************************************/

zlog_category_t *zc;
int qh_zlog_init()
{
	int rc;

	rc = zlog_init("./Log/CONFIG/zlog.conf");
	//rc = zlog_init("./zlog.conf");
	if (rc) {
		printf("init failed\n");
		return -1;
	}

	zc = zlog_get_category("my_cat");
	if (!zc) {
		printf("get cat fail\n");
		zlog_fini();
		return -2;
	}
	return 0;
}

int main(int argc, char* argv[])
{
	int rc;
	
	printf("_________\n");
	qh_zlog_init();

	qh_zlog_debug(zc, "%s", "hello, debug");
	qh_zlog_info(zc, "hello, info");
	qh_zlog_notice(zc, "hello, notice");
	qh_zlog_warn(zc, "hello, warn");
	qh_zlog_error(zc, "hello, error");
	qh_zlog_fatal(zc, "hello, fatal");



	
	signal(SIGINT,signal_handler); 
	qh_zlog_info(zc, "Command line starting ...");
	CLI_activate();

	zlog_fini();
	return 0;
}

